package com.probiz.estore.common.model.product;

import java.util.ArrayList;
import java.util.List;

import com.probiz.estore.common.model.product.base.ProductTypeTbl;

/**
 * Model class for ProductType. Add not database mapped fileds in this class.
 * 商品类型
 */
public class ProductType extends ProductTypeTbl {


	/**
	 * 实体
	 */
	public static final Short	PRODUCT_KIND_ENTITY=new Short("1");
	
	/**
	 * 服务
	 */
	public static final Short	PRODUCT_KIND_SERVICE=new Short("2");
	
	/**
	 * 软件（下载）
	 */
	public static final Short	PRODUCT_KIND_SOFTWARE=new Short("3");
	

	/**
	 * 卡类产品
	 */
	public static final Short	PRODUCT_KIND_CARD=new Short("4");
	
  	/**
	 * Default Empty Constructor for class ProductType
	 */
	public ProductType () {
		super();
	}
	
	/**
	 * Default Key Fields Constructor for class ProductType
     * @param in_productTypeId 类型id
	 */
	public ProductType (Integer in_productTypeId) {
		super (in_productTypeId);
	}
	
	//获得该类型所关联的品牌
	public List<Brand> getBrands(){
		List<Brand> brandList = new ArrayList<Brand>();
		for(ProductTypeBrand productTypeBrand : this.getProductTypeBrands()){
			brandList.add(productTypeBrand.getBrand());
		}
		return brandList;
		
	}
	/**
	 * 所属类型的产品的价格区间列表
	 * @return 所属类型的产品的价格区间列表，例如 100元以下 100元到500元 500元以上
	 */
	public List<PriceInterval> getPriceIntervalList() {
		List<PriceInterval> result = null;
		
		String intervals = trimPriceIntervals(getPriceIntervals());
		if (intervals != null && intervals.length() > 0) {
			String[] intervalExpressions = intervals.split(" ");
			if (intervalExpressions != null && intervalExpressions.length > 0) {
				result = new ArrayList<PriceInterval>();
                for (String intervalExpression : intervalExpressions) {
                    if (PriceInterval.isValidExpression(intervalExpression)) {
                        result.add(new PriceInterval(intervalExpression));
                    }
                }
				if (result.isEmpty()) {
					result = null;
				}
			}
		}
		
		return result;
	}
	
	/**
	 * To test the given price intervals 's validity
	 * @param priceIntervals  价格区间
	 * @return true if the given priceIntervals is valid
	 */
	public static boolean testPriceIntervals(String priceIntervals) {
		if (priceIntervals == null || priceIntervals.length() < 1) {
			return true;
		}
		
		String intervals = trimPriceIntervals(priceIntervals);
		String[] intervalExpressions = intervals.split(" ");
		if (intervalExpressions != null && intervalExpressions.length > 0) {
            for (String intervalExpression : intervalExpressions) {
                if (!PriceInterval.isValidExpression(intervalExpression)) {
                    return false;
                }
            }
		}
		
		return true;
	}
	
	public static String trimPriceIntervals(String priceIntervals) {
		if (priceIntervals == null || priceIntervals.length() < 1) {
			return null;
		}
		
		String result = priceIntervals;
		result = result.replaceAll("，", ",");
		result = result.replaceAll(" *, *", ",");			//eliminate whitespace around commas sign
		result = result.replaceAll(" *\\) *\\[ *", ") [");	//eliminate whitespace and make sure each pair of intervals are separated by one space 
		result = result.replaceAll(" *\\) *\\( *", ") (");
		result = result.replaceAll(" *\\] *\\( *", "] (");
		result = result.replaceAll(" *\\] *\\[ *", "] [");
		
		return result;
	}

}
